package com.xoms.admin.controller.springmvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;
import com.xoms.admin.controller.res.*;
import com.xoms.admin.controller.res.bean.*;
import com.xoms.admin.controller.springmvc.req.*;

/**
 * 部门相关接口
 **/
@Controller
@RequestMapping(value = "/dept", produces = "application/json")
public class SDeptController {
	@Autowired
	com.xoms.admin.controller.DeptController deptController;

	/**
	 * 增加/修改部门信息
	 **/
	@RequestMapping("/v1/edit")
	@ResponseBody
	public ObjectResult<RDepartmentBean> edit(@Validated EditDepartmentParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RDepartmentBean> res = new ObjectResult<RDepartmentBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return deptController.edit(param, request, response);
	}

	/**
	 * 删除部门信息
	 **/
	@RequestMapping("/v1/del")
	@ResponseBody
	public BaseResult del(@Validated DelDepartmentParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return deptController.del(param, request, response);
	}

	/**
	 * 显示所有部门信息
	 **/
	@RequestMapping("/v1/getDepts")
	@ResponseBody
	public ListResult<RDepartmentBean> getDepts(@Validated GetDeptsParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ListResult<RDepartmentBean> res = new ListResult<RDepartmentBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return deptController.getDepts(param, request, response);
	}

	/**
	 * 启用部门
	 **/
	@RequestMapping("/v1/enable")
	@ResponseBody
	public BaseResult enable(@Validated EnableDepartmentParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return deptController.enable(param, request, response);
	}

	/**
	 * 禁用部门
	 **/
	@RequestMapping("/v1/disable")
	@ResponseBody
	public BaseResult disable(@Validated DisableDepartmentParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return deptController.disable(param, request, response);
	}
}
